﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// qt headers
#include <QPainter>
#include <QFontMetrics>
#include <QMouseEvent>
#include <QEvent>
#include <QApplication>
#include <QMessageBox>
#include <QDir>
#include <QProgressBar>

// gcmp headers
#include "IJournalCommandData.h"
#include "JournalUtils.h"
#include "CommandParameters.h"

//custom headers
#include "ProjectComponentsListItemDelegate.h"
#include "ProjectComponentsListModel.h"
#include "IGmepGcsProjectManager.h"
#include "IProjectComponent.h"
#include "ProjectCloudComponentService.h"
#include "ProjectLoadedComponentService.h"
#include "PathUtils.h"
#include "PaintingUtils.h"
#include "UIService.h"
#include "MessageDialog.h"
#include "UIService.h"
#include "GcsMainWindow.h"
#include "FamilyUtils.h"
#include "AppService.h"
#include "CustomReply.h"
#include "BnufBulletin.h"
#include "Permission.h"
#include "PermissionWatcher.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs 
{
    ListItemDelegate::ListItemDelegate(QWidget *parent) : 
        QStyledItemDelegate(parent),
        m_parentWidget(parent),
        m_mouseHover(false),
        m_mouseCurrentRow(-1),
        m_IsOverButtonL(false),
        m_IsOverButtonR(false),
        m_isEditOperationVisible(true)
    {

    }

    ListItemDelegate::~ListItemDelegate()
    {

    }

    void ListItemDelegate::SetIsEditOperationVisible(bool isEditOperationVisible/* = true*/)
    {
        m_isEditOperationVisible = isEditOperationVisible;
    }

    void ListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {        
        IProjectComponent *component = static_cast<IProjectComponent*>(index.internalPointer());

        if (!component)
        {
            return;
        }

        const QAbstractItemModel * indexModel = index.model();
        if(!indexModel)
        {
            return;
        }

        painter->save();
        painter->setPen(QPen(Qt::white));
        painter->setBrush(Qt::white);

        QRectF rectItem;
        rectItem.setX(option.rect.x());
        rectItem.setY(option.rect.y());
        rectItem.setWidth(option.rect.width()-1);
        rectItem.setHeight(option.rect.height()-1);
        painter->drawRect(rectItem);
        painter->restore();
        int status = indexModel->data(index, Qt::UserRole).value<int>();
        QRect rect = QRect(option.rect.left() + option.rect.width() - 128, option.rect.top(), 128, 136);
        if ((m_mouseCurrentRow == index.row()) && m_mouseHover)
        {
            painter->save();
            painter->drawLine(rect.topLeft(), rect.topRight());
            painter->drawLine(rect.topRight(), rect.bottomRight());
            painter->drawLine(rect.bottomRight(), rect.bottomLeft());
            painter->drawLine(rect.bottomLeft(), rect.topLeft());
            painter->restore();
        }
        else
        {
            painter->save();
            painter->drawLine(rect.topLeft(), rect.topRight());
            painter->drawLine(rect.topRight(), rect.bottomRight());
            painter->drawLine(rect.bottomRight(), rect.bottomLeft());
            painter->drawLine(rect.bottomLeft(), rect.topLeft());
            painter->restore();
        }
        QRect checkedRect = QRect(option.rect.left() + 6, option.rect.top() + 6, 12, 12);
        // 构件名称
        QRect textRect = QRect(option.rect.left() + option.rect.width() - 128, option.rect.top() + option.rect.height() - 24, 128, 24);
        QString name = indexModel->data(index, Qt::DisplayRole).value<QString>();
        int textAlignment = indexModel->data(index, Qt::ItemDataRole::TextAlignmentRole).value<int>();
        QColor textColor = indexModel->data(index, Qt::ItemDataRole::TextColorRole).value<QColor>();
        QColor textUpdate = QColor("#5291DD");
        QPalette palette = QApplication::palette();
        if (status == 3)
        {
            palette.setColor(QPalette::ColorRole::Foreground, textUpdate);
        }
        else
        {
            palette.setColor(QPalette::ColorRole::Foreground, textColor);
        }
        QStyle* pStyle = QApplication::style();
        if (!pStyle)
        {
            return;
        }
        pStyle->drawItemText(painter, textRect, textAlignment, palette, true, name, QPalette::ColorRole::Foreground);
        // 构件图片
        QRect imageRect = QRect(option.rect.left() + 16, option.rect.top() + 8, 96, 96);
        QIcon icon = indexModel->data(index, Qt::DecorationRole).value<QIcon>();
        // 删除按钮,图标
        QRect deleteBucketRect = QRect(option.rect.left() + 104, option.rect.top() + 8, 16, 16);

        if (!icon.isNull())
        {
            painter->drawPixmap(imageRect.adjusted(1, 1, -1, -1), icon.pixmap(QSize(96, 96)));
        }

        if (option.state.testFlag(QStyle::State_Selected))
        {
            painter->save();
            painter->setPen(QPen("#5291DD"));
            painter->setBrush(QColor(229, 241, 255));

            QRectF rect;
            rect.setX(option.rect.x());
            rect.setY(option.rect.y());
            rect.setWidth(option.rect.width() - 1);
            rect.setHeight(option.rect.height() - 1);
            painter->drawLine(rect.topLeft(), rect.topRight());
            painter->drawLine(rect.topRight(), rect.bottomRight());
            painter->drawLine(rect.bottomRight(), rect.bottomLeft());
            painter->drawLine(rect.bottomLeft(), rect.topLeft());
            painter->restore();

            // 可编辑状态, checkbox， 删除
            if (m_isEditOperationVisible)
            {
                painter->save();
                painter->setPen(QPen("#BDC7D5"));
                painter->setBrush(Qt::NoBrush);
                bool isChecked = indexModel->data(index, Qt::CheckStateRole).value<bool>();


                // 绘制 checkbox
                if (isChecked)
                {
                    PaintingUtils::DrawBorder(painter, checkedRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(82, 145, 221, 255), QColor(82, 145, 221, 255));

                    QRect tickRect = checkedRect.marginsAdded(QMargins(-2, -2, -2, -2));
                    QImage tickImage = QImage(":/images/icon_tick_ffffff_12.png");
                    painter->setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);
                    painter->drawPixmap(tickRect, QPixmap::fromImage(tickImage));
                }
                else
                {
                    PaintingUtils::DrawBorder(painter, checkedRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(189, 199, 213, 255));
                }
                painter->restore();

                // 删除按钮图标
                if (0 != status)
                {
                    QString deleteIconAddress = ":/images/delete_bucket_#FF6E6E_16x16.png";
                    if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                    {
                        deleteIconAddress = ":/images/delete_bucket_gray_16x16.png";
                    }
                    QImage deleteIcon(deleteIconAddress);
                    deleteIcon.scaled(16, 16, Qt::KeepAspectRatio);
                    painter->drawImage(deleteBucketRect, deleteIcon);
                }
            }
            DrawButtonByStatus(painter, option, status);
        }
        else if (option.state.testFlag(QStyle::State_MouseOver))
        {
            painter->save();
            painter->setPen(QPen("#5291DD"));
            painter->setBrush(Qt::NoBrush);

            QRectF rect;
            rect.setX(option.rect.x());
            rect.setY(option.rect.y());
            rect.setWidth(option.rect.width() - 1);
            rect.setHeight(option.rect.height() - 1);
            painter->drawLine(rect.topLeft(), rect.topRight());
            painter->drawLine(rect.topRight(), rect.bottomRight());
            painter->drawLine(rect.bottomRight(), rect.bottomLeft());
            painter->drawLine(rect.bottomLeft(), rect.topLeft());
            painter->restore();

            // 可编辑状态, checkbox， 删除
            if (m_isEditOperationVisible)
            {
                painter->save();
                painter->setPen(QPen("#BDC7D5"));
                painter->setBrush(Qt::NoBrush);
                bool isChecked = indexModel->data(index, Qt::CheckStateRole).value<bool>();

                // 绘制 checkbox
                if (isChecked)
                {
                    PaintingUtils::DrawBorder(painter, checkedRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(82, 145, 221, 255), QColor(82, 145, 221, 255));

                    QRect tickRect = checkedRect.marginsAdded(QMargins(-2, -2, -2, -2));
                    QImage tickImage = QImage(":/images/icon_tick_ffffff_12.png");
                    painter->setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);
                    painter->drawPixmap(tickRect, QPixmap::fromImage(tickImage));
                }
                else
                {
                    PaintingUtils::DrawBorder(painter, checkedRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(189, 199, 213, 255));
                }
                painter->restore();

                // 删除按钮图标
                if (0 != status)
                {
                    QString deleteIconAddress = ":/images/delete_bucket_#FF6E6E_16x16.png";
                    if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                    {
                        deleteIconAddress = ":/images/delete_bucket_gray_16x16.png";
                    }
                    QImage deleteIcon(deleteIconAddress);
                    deleteIcon.scaled(16, 16, Qt::KeepAspectRatio);
                    painter->drawImage(deleteBucketRect, deleteIcon);
                }
            }
            DrawButtonByStatus(painter, option, status);
        }
        else {
            painter->save();
            painter->setPen(QPen(Qt::white));
            painter->setBrush(Qt::white);

            QRectF rectItem;
            rectItem.setX(option.rect.x());
            rectItem.setY(option.rect.y());
            rectItem.setWidth(option.rect.width() - 1);
            rectItem.setHeight(option.rect.height() - 1);
            painter->drawLine(rectItem.topLeft(), rectItem.topRight());
            painter->drawLine(rectItem.topRight(), rectItem.bottomRight());
            painter->drawLine(rectItem.bottomRight(), rectItem.bottomLeft());
            painter->drawLine(rectItem.bottomLeft(), rectItem.topLeft());
            painter->restore();

            // 可编辑状态, checkbox， 删除
            if (m_isEditOperationVisible)
            {
                bool isChecked = indexModel->data(index, Qt::CheckStateRole).value<bool>();

                // 绘制 checkbox
                if (isChecked)
                {
                    PaintingUtils::DrawBorder(painter, checkedRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(82, 145, 221, 255), QColor(82, 145, 221, 255));
                    QRect tickRect = checkedRect.marginsAdded(QMargins(-2, -2, -2, -2));
                    QImage tickImage = QImage(":/images/icon_tick_ffffff_12.png");
                    painter->setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);
                    painter->drawPixmap(tickRect, QPixmap::fromImage(tickImage));
                }

                painter->restore();
            }
        }
        if (component->IsReleased() && (component->GetIsSyncable() || component->GetIsUpdatable()))
        {
            painter->save();
            painter->setRenderHint(QPainter::RenderHint::Antialiasing);
            painter->setBrush(QBrush(QColor(255, 110, 110)));
            painter->setPen(QPen(QColor(255, 110, 110)));
            float radius = 3.5;
            QPointF center = option.rect.topRight() + QPointF(0, radius);
            painter->drawEllipse(center, radius, radius);
            painter->restore();
        }
    }

    bool ListItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    {
        if (!index.isValid() || !model)
        {
            return false;
        }

        const QAbstractItemModel * indexModel = index.model();
        if (!indexModel)
        {
            return false;
        }
        IProjectComponent *component = static_cast<IProjectComponent *>(index.internalPointer());
        if (!component)
        {
            return false;
        }

        if (!m_parentWidget)
        {
            return QStyledItemDelegate::editorEvent(event, model, option, index);
        }
        m_parentWidget->setCursor(Qt::ArrowCursor);
        m_parentWidget->setToolTip(QString());

        QRect decorationRect(option.rect.left() + option.rect.width() - 30, option.rect.top(), 30, 30);
        QRect checkStateRect(option.rect.left() + 6, option.rect.top() + 6, 12, 12);
        QRect deleteRect = QRect(option.rect.left() + 68, option.rect.top() + 88, 52, 24);
        QRect deployRect = QRect(option.rect.left() + 8, option.rect.top() + 88, 52, 24);
        QRect loadRect = QRect(option.rect.left() + 68, option.rect.top() + 88, 52, 24);
        QRect loadedRect = QRect(option.rect.left() + 24, option.rect.top() + 88, 80, 24);
        QRect updateRect = QRect(option.rect.left() + 24, option.rect.top() + 88, 80, 24);
        QRect deleteBucketRect = QRect(option.rect.left() + 104, option.rect.top() + 8, 16, 16);
        QRect deleteHandRect = QRect(option.rect.left() + 96, option.rect.top(), 32, 32);
        QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
        int status = indexModel->data(index, Qt::UserRole).value<int>();

        if (event->type() == QMouseEvent::MouseButtonPress)
        {
            if (mouseEvent)
            {
                if (0 == status)//布置和删除按钮
                {
                    if (deployRect.contains(mouseEvent->pos()))
                    {
                        emit PlaceComponentRequired(model, index);
                    }
                    if (m_isEditOperationVisible)
                    {
                        if (deleteRect.contains(mouseEvent->pos()))
                        {
                            if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                            {
                                return QStyledItemDelegate::editorEvent(event, model, option, index);
                            }

                            emit DeleteComponentRequired(model, index);
                        }
                    }
                }
                else if (1 == status)//布置和载入按钮
                {
                    if (deployRect.contains(mouseEvent->pos()))
                    {
                        emit PlaceComponentRequired(model, index);
                    }
                    if (m_isEditOperationVisible)
                    {
                        if (loadRect.contains(mouseEvent->pos()))
                        {
                            emit LoadComponentRequired(model, index);
                        }
                        if (deleteBucketRect.contains(mouseEvent->pos()))
                        {
                            if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                            {
                                return QStyledItemDelegate::editorEvent(event, model, option, index);
                            }

                            emit DeleteComponentRequired(model, index);
                        }
                    }
                }
                else if (2 == status)//仅载入按钮
                {
                    if (m_isEditOperationVisible)
                    {
                        if (loadedRect.contains(mouseEvent->pos()))
                        {
                            emit LoadComponentRequired(model, index);
                        }
                        if (deleteBucketRect.contains(mouseEvent->pos()))
                        {
                            if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                            {
                                return QStyledItemDelegate::editorEvent(event, model, option, index);
                            }
                            emit DeleteComponentRequired(model, index);
                        }
                    }
                }
                else if (3 == status)//仅更新按钮
                {
                    if (m_isEditOperationVisible)
                    {
                        if (updateRect.contains(mouseEvent->pos()))
                        {
                            emit SyncComponentRequired(model, index);   
                        }
                        if (deleteBucketRect.contains(mouseEvent->pos()))
                        {
                            if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                            {
                                return QStyledItemDelegate::editorEvent(event, model, option, index);
                            }
                            emit DeleteComponentRequired(model, index);
                        }
                    }
                }

                if (checkStateRect.contains(mouseEvent->pos()))
                {
                    QModelIndex itemIndex = model->index(index.row(), 0, index.parent());
                    const QAbstractItemModel * itemIndexModel = itemIndex.model();
                    if (!itemIndexModel)
                    {
                        return false;
                    }
                    bool isChecked = itemIndexModel->data(index, Qt::CheckStateRole).value<bool>();
                    model->setData(index, !isChecked, Qt::CheckStateRole);
                }
            }
        }
        if (event->type() == QMouseEvent::MouseMove)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            if (option.rect.contains(mouseEvent->pos()))
            {
                setMouseHover(true, model, index);
            }
            else
            {
                setMouseHover(false, model, index);
            }


            if (m_isEditOperationVisible)
            {
                if (0 == status)
                {
                    if (deleteRect.contains(mouseEvent->pos()))
                    {
                        if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                        {
                            m_parentWidget->setToolTip(QString::fromStdWString(GBMP_TR(L"示例项目中无该功能权限")));
                            return QStyledItemDelegate::editorEvent(event, model, option, index);
                        }

                        m_IsOverButtonR = true;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else if (deployRect.contains(mouseEvent->pos()))
                    {
                        m_IsOverButtonL = true;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else
                    {
                        m_IsOverButtonL = false;
                        m_IsOverButtonR = false;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::ArrowCursor);
                    }
                }
                else if (1 == status)
                {
                    if (deleteBucketRect.contains(mouseEvent->pos()))
                    {
                        if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                        {
                            m_parentWidget->setToolTip(QString::fromStdWString(GBMP_TR(L"示例项目中无该功能权限")));
                            return QStyledItemDelegate::editorEvent(event, model, option, index);
                        }

                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else if (deployRect.contains(mouseEvent->pos()))
                    {
                        m_IsOverButtonL = true;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else if (loadRect.contains(mouseEvent->pos()))
                    {
                        m_IsOverButtonR = true;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else
                    {
                        m_IsOverButtonL = false;
                        m_IsOverButtonR = false;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::ArrowCursor);
                    }
                }
                else if (2 == status)
                {
                    if (deleteBucketRect.contains(mouseEvent->pos()))
                    {
                        if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                        {
                            m_parentWidget->setToolTip(QString::fromStdWString(GBMP_TR(L"示例项目中无该功能权限")));
                            return QStyledItemDelegate::editorEvent(event, model, option, index);
                        }

                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else if (deployRect.contains(mouseEvent->pos()))
                    {
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else
                    {
                        m_parentWidget->setCursor(Qt::ArrowCursor);
                    }
                }
                else if (3 == status)
                {
                    if (deleteBucketRect.contains(mouseEvent->pos()))
                    {
                        if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                        {
                            m_parentWidget->setToolTip(QString::fromStdWString(GBMP_TR(L"示例项目中无该功能权限")));
                            return QStyledItemDelegate::editorEvent(event, model, option, index);
                        }

                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else if (updateRect.contains(mouseEvent->pos()))
                    {
                        m_IsOverButtonL = true;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else
                    {
                        m_IsOverButtonL = false;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::ArrowCursor);
                    }
                }
            }
            else
            { 
                // 仅处理布置button
                if (0 == status || 1 == status)
                {
                    if (deployRect.contains(mouseEvent->pos()))
                    {
                        m_IsOverButtonL = true;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::PointingHandCursor);

                    }
                    else
                    {
                        m_IsOverButtonL = false;
                        m_IsOverButtonR = false;
                        emit model->dataChanged(index, index);
                        m_parentWidget->setCursor(Qt::ArrowCursor);
                    }
                }
                else if (2 == status)
                {
                    if(deployRect.contains(mouseEvent->pos()))
                    {
                        m_parentWidget->setCursor(Qt::PointingHandCursor);
                    }
                    else
                    {
                        m_parentWidget->setCursor(Qt::ArrowCursor);
                    }
                }
                else if (3 == status)
                {
                    m_IsOverButtonL = false;
                    emit model->dataChanged(index, index);
                    m_parentWidget->setCursor(Qt::ArrowCursor);
                }
            }
        }

        return QStyledItemDelegate::editorEvent(event, model, option, index);
    }

    void ListItemDelegate::setMouseHover(bool mouseHover, QAbstractItemModel *model, const QModelIndex &index)
    {
        ListItemModel *listItemModel = dynamic_cast<ListItemModel*>(model);
        if (listItemModel == 0)
        {
            return;
        }
        if (m_mouseCurrentRow != index.row() && m_mouseCurrentRow >= 0)
        {
            //listItemModel->updateRow(m_mouseCurrentRow);
        }
        m_mouseCurrentRow = index.row();

        if (m_mouseHover != mouseHover)
        {
            m_mouseHover = mouseHover;
            //listItemModel->updateRow(index.row());
        }
    }

    void ListItemDelegate::DrawButtonByStatus(QPainter *painter, const QStyleOptionViewItem &option, int status) const
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(painter, L"painter为空", L"GMEP", L"20221028");

        bool isHasPlaceBtn = false; // 布置按钮
        QRect rect;
        QString text;
        QPalette palette = QApplication::palette();

        if (m_isEditOperationVisible)
        {
            painter->save();
            if (0 == status)
            {
                isHasPlaceBtn = true;
                // 删除按钮
                text = QString::fromStdWString(GBMP_TR(L"删除"));
                rect = QRect(option.rect.left() + 68, option.rect.top() + 88, 52, 24);
                if (m_IsOverButtonR)
                {
                    if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                    {
                        painter->setPen(QPen(QColor("#BDC7D5")));
                    }
                    else
                    {
                        painter->setPen(QPen(QColor("#FF6E6E")));
                    }
                }
                else
                {
                    painter->setPen(QPen(QColor("#BDC7D5")));
                }
                painter->setBrush(QBrush(Qt::white));
                if (PermissionWatcher::Get()->GetCurrentPermission() == Permission::SOFTWRITE)
                {
                    palette.setColor(QPalette::ColorRole::Foreground, QColor(120, 122, 131, 255));
                }
                else
                {
                    palette.setColor(QPalette::ColorRole::Foreground, QColor("#FF6E6E"));
                }
            }
            else if (1 == status || 3 == status)
            {
                isHasPlaceBtn = true;
                // 载入按钮
                text = QString::fromStdWString(GBMP_TR(L"载入"));
                rect = QRect(option.rect.left() + 68, option.rect.top() + 88, 52, 24);
                if (m_IsOverButtonR)
                {
                    painter->setPen(QPen(QColor("#00B1D1")));
                }
                else
                {
                    painter->setPen(QPen(QColor("#BDC7D5")));
                }
                painter->setBrush(QBrush(Qt::white));
                palette.setColor(QPalette::ColorRole::Foreground, QColor("#00B1D1"));
            }
            else if (2 == status)
            {
                // 仅载入按钮
                text = QString::fromStdWString(GBMP_TR(L"载入"));
                rect = QRect(option.rect.left() + 24, option.rect.top() + 88, 80, 24);
                painter->setPen(Qt::NoPen);
                painter->setBrush(QBrush(QColor("#5291DD")));
                palette.setColor(QPalette::ColorRole::Foreground, Qt::white);
            }
            DrawButton(painter, rect, palette, text);
            painter->restore();

        }
        else // 本地版仅绘制布置按钮
        {
            isHasPlaceBtn = true;
        }

        if (isHasPlaceBtn)
        {
            painter->save();
            rect = QRect(option.rect.left() + 8, option.rect.top() + 88, 52, 24);
            if (m_IsOverButtonL)
            {
                painter->setPen(QPen(QColor("#00B1D1")));
            }
            else
            {
                painter->setPen(QPen(QColor("#BDC7D5")));
            }
            painter->setBrush(QBrush(Qt::white));
            palette.setColor(QPalette::ColorRole::Foreground, QColor("#00B1D1"));
            DrawButton(painter, rect, palette, QString::fromStdWString(GBMP_TR(L"布置")));
            painter->restore();

        }
    }

    void ListItemDelegate::DrawButton(QPainter *painter, const QRect &rect, const QPalette &palette, const QString &text) const
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(painter, L"painter为空", L"GMEP", L"20221028");

        painter->drawLine(rect.topLeft(), rect.topRight());
        painter->drawLine(rect.topRight(), rect.bottomRight());
        painter->drawLine(rect.bottomRight(), rect.bottomLeft());
        painter->drawLine(rect.bottomLeft(), rect.topLeft());
        painter->drawRoundedRect(rect, 2, 2);

        QStyle* pStyle = QApplication::style();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pStyle, L"pStyle为空", L"GMEP", L"20221028");
        pStyle->drawItemText(painter, rect, Qt::AlignCenter, palette, true, text, QPalette::ColorRole::Foreground);
    }
}
